package agent.keskkonna.behaviour;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.util.Logger;

import java.util.logging.Level;

import agent.Location;
import agent.keskkonna.KeskkonnaAgent;
import agent.ontology.LocationOntology;
import agent.params.LocationParam;
import agent.params.PassangerParam;
import agent.params.SpeedParam;
import agent.teekonna.TeekonnaAgent;
import agent.util.AgentUtil;

public class KeskkonnaCyclicBehaviour extends CyclicBehaviour {

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger
			.getJADELogger(KeskkonnaCyclicBehaviour.class.getName());

	public KeskkonnaCyclicBehaviour(KeskkonnaAgent myAgent) {
		super(myAgent);
	}

	@Override
	public void action() {
		ACLMessage msg = myAgent.receive();
		if (msg == null) {
			block();
		} else {
			try {
				// kui päring tuleb Teekonna Agendilt
				if(msg.getSender().equals(AgentUtil.createLocalAid(TeekonnaAgent.class))){
					ACLMessage reply=getLocationAverageSpeedMessage(msg);
					myAgent.send(reply);
				}
				else if (msg.getOntology().equals(LocationOntology.GPS.toString())) {
					ACLMessage reply = new ACLMessage(ACLMessage.INFORM);
					reply.addUserDefinedParameter(LocationParam.LOCATION_GPS_E.name(), "GPS_E");
					reply.addUserDefinedParameter(LocationParam.LOCATION_GPS_N.name(), "GPS_N");
					reply.addUserDefinedParameter(PassangerParam.PASSANGER_ID.name(), msg.getUserDefinedParameter(PassangerParam.PASSANGER_ID.name()));
					reply.setReplyWith(msg.getReplyWith());
					reply.addReceiver(msg.getSender());
					myAgent.send(reply);
					LOG.info("Keskkonna agendilt päritakse kasutaja asukohta");
				}
			} catch (Exception ex) {
				LOG.log(Level.SEVERE, STATE_BLOCKED, ex);
			}
		}
	}

	private ACLMessage getLocationAverageSpeedMessage(ACLMessage msg) {
		ACLMessage reply=new ACLMessage(ACLMessage.INFORM);
		Location from=new Location(msg.getUserDefinedParameter(LocationParam.LOCATION_FROM_GPS_N.toString()),msg.getUserDefinedParameter(LocationParam.LOCATION_FROM_GPS_E.toString()));
		Location to=new Location(msg.getUserDefinedParameter(LocationParam.LOCATION_GPS_N.toString()),msg.getUserDefinedParameter(LocationParam.LOCATION_GPS_E.toString()));
		reply.addUserDefinedParameter(SpeedParam.AVG_SPEED.toString(), calculateAvgSpeed(from,to));
		reply.addUserDefinedParameter(LocationParam.LOCATION_FROM_GPS_N.toString(), from.getGpsN());
		reply.addUserDefinedParameter(LocationParam.LOCATION_FROM_GPS_E.toString(), from.getGpsE());
		reply.addUserDefinedParameter(LocationParam.LOCATION_GPS_N.toString(), to.getGpsN());
		reply.addUserDefinedParameter(LocationParam.LOCATION_GPS_E.toString(), to.getGpsE());
		reply.setReplyWith(msg.getReplyWith());
		reply.addReceiver(AgentUtil.createLocalAid(TeekonnaAgent.class));
		return reply;
	}

	private String calculateAvgSpeed(Location from, Location to) {
		LOG.log(Level.INFO,"Calculating average speed between "+from.getGpsE()+" "+from.getGpsN()+ " and "+to.getGpsE()+" "+to.getGpsN()+".");
		return "25";
	}

}
